package com.yantong.service.impl;

import com.yantong.utils.PageUtils;
import com.yantong.pojo.Shipcard;
import com.yantong.pojo.ShipcardRecord;
import com.yantong.mapper.ShipcardMapper;
import com.yantong.mapper.ShipcardRecordMapper;
import com.yantong.service.ShipcardService;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 会员充卡
 * @author xiangyi
 * @date 2018/9/8.20:10
 */

@Component
public class ShipcardServiceImpl implements ShipcardService {

    private static final Logger logger = LoggerFactory.getLogger(ShipcardServiceImpl.class);


    @Autowired
    private  ShipcardMapper shipcardMapper;
    
    @Autowired
    private  ShipcardRecordMapper shipcardRecordMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public   PageUtils<Shipcard> list(Shipcard params, int offset, int limit) {
    	 PageUtils<Shipcard> pageUtils = new PageUtils<>();
    	 int total = shipcardMapper.getTotal(params);
    	 params.setQueryitem(params.getQueryitem().trim());
    	 if(total>0) {
    	   pageUtils.setTotal(total);
    	   pageUtils.setRows(shipcardMapper.list(params,new RowBounds(offset, limit)));
    	 }else {
    		 logger.info("没有数据");
    	 }
         return pageUtils;
    }
    
    /**
     * 查询
     * @param params
     * @return
     */
    public Shipcard list(Shipcard params) {
    	return shipcardMapper.list(params);
    };
    
    
    /**
     * 添加
     * @param params
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public  int save(Shipcard params) {
    	return shipcardMapper.insertSelective(params);
    };
    
    
    /**
     * 删除
     * @param params
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int remove(Shipcard params) {
    	return shipcardMapper.deleteByPrimaryKey(params.getId());
    };
    
    /**
     * 更新
     * @param params
     * @return
     */
    @Override
    public  int update(Shipcard params) {
    	return shipcardMapper.updateByPrimaryKeySelective(params);
    };
    
    /**
     * 查询购物卡
     * @param record
     * @return
     */
    public List<Shipcard> lists(Shipcard record){
    	return shipcardMapper.lists(record);
    };
    
    /**
     * 得到会员详情
     * @param record
     * @return
     */
    public List<ShipcardRecord> getShipcardByMid(ShipcardRecord record){
    	return shipcardRecordMapper.getShipcardByMid(record);
    };
    
    /**
     * 获取详情
     * @param id
     * @return
     */
    public Shipcard selectByPrimaryKey(Long id){
    	return shipcardMapper.selectByPrimaryKey(id);
    };
    
    /**
     * 总页数
     * @param record
     * @return
     */
    public  int getTotal(ShipcardRecord record){
    	return shipcardRecordMapper.getTotal(record);
   };
   
   /**
    * 得到充值卡定时任务数据
    * @return
    */
   public  List<ShipcardRecord> getTaskList(){
	   return shipcardRecordMapper.getTaskList();
   };
   
   /**
    * 得到会员卡购买详情
    * @param record
    * @return
    */
   public  PageUtils<ShipcardRecord> getShipcardById(ShipcardRecord record,int offset, int limit){
	   PageUtils<ShipcardRecord> pageUtils = new PageUtils<ShipcardRecord>();
	   int total = shipcardRecordMapper.getShipcardRecordTotal(record);
  	 	if(total>0) {
	  	   pageUtils.setTotal(total);
	  	   pageUtils.setRows(shipcardRecordMapper.getShipcardById(record,new RowBounds(offset, limit)));
  	 	}else {
  		  logger.info("没有数据");
  	 	}
	   return  pageUtils;
   };
}

